// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Globalization;
using System.Xml.Serialization;

namespace SoapTrace.Core.Formatters
{
	/// <summary>
	/// A web service operation's parameter.
	/// </summary>
	[ XmlType( "param" ) ]
	public class WebServiceParameter : ICloneable
	{
		/// <summary>
		/// The parameter type.
		/// </summary>
		private string m_ParameterType = String.Empty;
		
		/// <summary>
		/// The parameter's name.
		/// </summary>
		private string m_Name = String.Empty;
		
		/// <summary>
		/// A flag indicating whether to display the parameter in the GUI or not.
		/// </summary>
		private bool m_Display = true;
		
		/// <summary>
		/// String that indicates that the parameter is an array.
		/// </summary>
		private static string ArrayTypeIndicator = ".array";
		
		/// <summary>
		/// The array items associated with this parameter.
		/// </summary>
		private WebServiceArrayItemCollection m_ArrayItems = 
			new WebServiceArrayItemCollection( );		

		/// <summary>
		/// Creates a new instance of the <see cref="WebServiceParameter"/>
		/// class.
		/// </summary>
		public WebServiceParameter( )
		{
		}

		/// <summary>
		/// Gets or sets the parameter type.
		/// </summary>
		[ XmlAttribute( "type" ) ]
		public string ParameterType
		{
			get
			{
				return m_ParameterType;
			}

			set
			{
				m_ParameterType = value;
			}
		}

		/// <summary>
		/// Gets or sets the parameter name.
		/// </summary>
		[ XmlAttribute( "name" ) ]
		public string Name
		{
			get
			{
				return m_Name;
			}

			set
			{
				m_Name = value;
			}
		}

		/// <summary>
		/// Gets or sets a flag indicating whether to display
		/// this parameter or not.
		/// </summary>
		[ XmlAttribute( "display" ) ]
		public bool Display
		{
			get
			{
				return m_Display;
			}

			set
			{
				m_Display = value;
			}
		}

		/// <summary>
		/// Gets or sets the array items associated with this parameter.
		/// </summary>
		[ XmlElement( "item" ) ]
		public WebServiceArrayItemCollection Items
		{
			get
			{
				return m_ArrayItems;
			}
		}

		
		/// <summary>
		/// Gets or sets a flag indicating whether this parameter is an
		/// array.
		/// </summary>
		[ XmlIgnore ]
		public bool IsArray
		{
			get
			{
				return GetIsArray( );
			}
		}	
		
		/// <summary>
		/// Gets or sets the array parameter type.
		/// </summary>
		[ XmlIgnore ]
		public string ArrayType
		{
			get
			{
				return GetArrayType( );
			}
		}		
		
		#region ICloneable Members

		/// <summary>
		/// Takes a deep copy of this class.
		/// </summary>
		/// <returns></returns>
		public object Clone( )
		{
			WebServiceParameter Parameter = new WebServiceParameter( );
			Parameter.Name = String.Copy( Name );
			Parameter.ParameterType = String.Copy( ParameterType );
			Parameter.Display = Display;

			return Parameter;
		}

		#endregion
		
		/// <summary>
		/// Gets whether this parameter is an array.
		/// </summary>
		/// <returns>A flag indicating whether this parameter
		/// is an array.</returns>
		private bool GetIsArray( )
		{
			bool IsArray = false;
			
			if( m_ParameterType.ToLower( CultureInfo.InvariantCulture ).EndsWith( WebServiceParameter.ArrayTypeIndicator ) )
			{
				IsArray = true;
			}
			
			return IsArray;
		}
		
		/// <summary>
		/// Gets whether the array type.
		/// </summary>
		/// <returns>The array parameter type.</returns>
		private string GetArrayType( )
		{
			string ArrayType = String.Empty;
			
			if( IsArray )
			{
				int Index = m_ParameterType.IndexOf( WebServiceParameter.ArrayTypeIndicator );
				
				ArrayType = m_ParameterType.Substring( 0, Index );
			}
			
			return ArrayType;
		}		
	}
}
